package com.masu.knows.sys.security;


import com.masu.knows.sys.interceptor.AuthInterceptor;
import com.masu.knows.sys.interceptor.DemoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//使用SpringMvc框架功能配置跨域
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*");
    }
    //配置拦截器
    @Autowired
    private DemoInterceptor demoInterceptor;
    @Autowired
    private AuthInterceptor authInterceptor;
    //设置拦截器生效路径
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor)
                //这个哪些路径经过这个拦截器
                .addPathPatterns("/v1/auth/demo");
        registry.addInterceptor(authInterceptor)
                .addPathPatterns(
                        "/v1/home",
                        "/v1/users/me"
                );
    }
}
